import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

/**
 * @Author lzc
 * @Date 2022/5/21 10:17
 */
public class Join_3 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        // 状态在内存中的保存时间
        tEnv.getConfig().setIdleStateRetention(Duration.ofSeconds(10));
        
        tEnv.executeSql("create table t1(" +
                            "id string, " +
                            "name string, " +
                            "pt as proctime()" +
                            ")with(" +
                            " 'connector' = 'kafka', " +
                            " 'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            " 'properties.group.id' = 'atguigu', " +
                            " 'topic' = 't1', " +
                            " 'format' = 'csv', " +
                            " 'scan.startup.mode' = 'latest-offset'" +
                            ")");
//        tEnv.sqlQuery("select * from t1").execute().print();
    
        tEnv.executeSql("create table t2(" +
                            "dic_code string, " +
                            "dic_name string, " +
                            "primary key(dic_code) not enforced " +
                            ")with(" +
                            " 'connector' = 'jdbc', " +
                            " 'url' = 'jdbc:mysql://hadoop162:3306/gmall2022?useSSL=false', " +
                            " 'table-name' = 'base_dic', " +
                            " 'username' = 'root', " +
                            "'lookup.cache.max-rows' = '10',\n" +  // 最多缓存10行
                            "'lookup.cache.ttl' = '1 minute',\n" +
                            " 'password' = 'aaaaaa' " +
                            ")");
        
//        tEnv.sqlQuery("select * from t2").execute().print();
        
        tEnv.sqlQuery("select" +
                          " t1.id, t1.name, " +
                          " t2.dic_name " +
                          "from t1 " +
                          "join t2 FOR SYSTEM_TIME AS OF t1.pt " +
                          "on t1.id=t2.dic_code")
            .execute()
            .print();
        
        
        
        
      
        
       
    }
}
